package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Student> listStudent(StudentQueryParam student) {
        Page<Student> page = new Page<>(student.getPage(), student.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        LambdaQueryWrapper< Student> lqw = new LambdaQueryWrapper<>();
        lqw.like(student.getName() != null && student.getName().length() > 0, Student::getName, student.getName())
                .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());
        this.page(page, lqw);
        List<Student> records = page.getRecords();
        Set<Integer> clazzIdSet = records.stream().map(Student::getClazzId).filter(Objects::nonNull).collect(Collectors.toSet());
        if (clazzIdSet != null && clazzIdSet.size() > 0){
            List<Clazz> clazzes = clazzService.listByIds(clazzIdSet);
            Map<Integer, String> collect = clazzes.stream().filter(Objects::nonNull).collect(Collectors.toMap(Clazz::getId, Clazz::getName));
            for (Student record : records) {
                record.setClazzName(collect.get(record.getClazzId()));
            }
        }
        PageResult<Student> pageResult = new PageResult<>(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        this.save(student);
    }

    @Override
    public Student getByClazzId(Integer id) {
        Student student = this.getById(id);
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        this.updateById(student);
    }

    @Override
    public void deleteStudentByIds(List<Integer> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void violationById(Integer id, Integer score) {
        LambdaUpdateWrapper<Student> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(Student::getId, id)
                .setSql("violation_score = violation_score + " + score)
                .setSql("violation_count=violation_count+"+1)
                .set(Student::getUpdateTime, LocalDateTime.now());
                this.update(lqw);
    }
}
